<?php

/**
 * 后台友情链接模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: link.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class linkAction extends Action
{

    private $tmp_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('link');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin');
    }

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {
        //TODO  取出所有资讯
        $rs = $this->tmp_model->getlinkList();
        $this->V('link');
    }

    /**
     * 新增/修改友情链接
     */
    public function add()
    {
        $link_id = !empty($_GET['link_id']) ? (int) $_GET['link_id'] : 0;
        if ($link_id > 0) {
            $editinfo = $this->tmp_model->getlinkInfo($link_id);
        } else {
            $editinfo = array();
            $editinfo['link_type_radio'] = '';
            $editinfo['link_state_radio'] = '';
            $editinfo['link_target'] = '_blank';
        }
        //取友情链接类型radiobutton数组
        $link_type_radio_ary = UtilityConfig::link_type_radio_ary();
        $link_type_radio_option = Utility::RadioButton($link_type_radio_ary, 'link_type_radio', $editinfo['link_type_radio'], 'onclick="typechange(this.value);"');

        //取广告状态option数组
        $link_state_radio_ary = UtilityConfig::ad_state_radio_ary();
        $link_state_radio_option = Utility::RadioButton($link_state_radio_ary, 'link_state_radio', $editinfo['link_state_radio'], 'onclick="statechange(this.value);"');

        //取友情链接打开类型数组
        $link_target_radio_ary = UtilityConfig::link_target_radio_ary();
        $link_target_radio_option = Utility::RadioButton($link_target_radio_ary, 'link_target_radio', $editinfo['link_target'], '');

        //初始化一下    默认state_radio
        $editinfo['link_order'] = !empty($editinfo['link_order']) ? $editinfo['link_order'] : '0';
        $editinfo['link_url'] = !empty($editinfo['link_url']) ? $editinfo['link_url'] : 'http://';
        $editinfo['link_starttime'] = !empty($editinfo['link_starttime']) ? date('Y-m-d', $editinfo['link_starttime']) : '';
        $editinfo['link_endtime'] = !empty($editinfo['link_endtime']) ? date('Y-m-d', $editinfo['link_endtime']) : '';
        $editinfo['link_name'] = !empty($editinfo['link_name']) ? $editinfo['link_name'] : '';
        $editinfo['link_id'] = !empty($editinfo['link_id']) ? $editinfo['link_id'] : '';
        $link_image = !empty($editinfo['link_image']) ? $editinfo['link_image'] : '';
        $editinfo['externallinks'] = $editinfo['link_type_radio'] == 3 ? $link_image : '';
        $editinfo['link_image'] = $editinfo['link_type_radio'] == 2 ? $link_image : '';
        $editinfo['link_description'] = !empty($editinfo['link_description']) ? $editinfo['link_description'] : '';
        $this->assign('link_type_radio_option', $link_type_radio_option);
        $this->assign('link_state_radio_option', $link_state_radio_option);
        $this->assign('link_target_radio_option', $link_target_radio_option);
        $this->assign('editinfo', $editinfo);

        $this->V('link');
    }

    /**
     * 新增/修改友情链接页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 5) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $link_id = !empty($_POST['link_id']) ? (int) $_POST['link_id'] : 0;
        $link_name = !empty($_POST['title']) ? trim($this->H($_POST['title'])) : '';
        $link_url = !empty($_POST['link']) ? trim($this->H($_POST['link'])) : '';
        $link_type_radio = !empty($_POST['link_type_radio']) ? $_POST['link_type_radio'] : '';
        $link_upload = !empty($_POST['thumb']) ? $_POST['thumb'] : '';
        $link_externallinks = !empty($_POST['externallinks']) ? trim($this->H($_POST['externallinks'])) : '';
        $link_order = !empty($_POST['order']) && is_numeric($_POST['order']) ? $_POST['order'] : '0';
        $link_state_radio = !empty($_POST['link_state_radio']) ? $_POST['link_state_radio'] : '';
        $link_starttime = !empty($_POST['starttime']) ? strtotime($_POST['starttime']) : '';
        $link_endtime = !empty($_POST['endtime']) ? strtotime($_POST['endtime']) : '';
        $link_target = !empty($_POST['link_target_radio']) ? $_POST['link_target_radio'] : '';
        $link_description = !empty($_POST['link_description']) ? $_POST['link_description'] : '';

        //判断友链类型
        if ($link_type_radio == 2) {
            $link_image = $link_upload;
        } elseif ($link_type_radio == 3) {
            $link_image = $link_externallinks;
        } else {
            $link_image = '';
        }

        /* 验证不能为空 */
        if (empty($link_name)) {
            $this->redirect('友情链接标题不能为空');
            exit;
        }
        if (empty($link_url)) {
            $this->redirect('友情链接不能为空');
            exit;
        }
        if (!isset($link_type_radio)) {
            $this->redirect('选择友情链接类型!');
            exit;
        }
        if (!isset($link_state_radio)) {
            $this->redirect('选择友情链接状态!');
            exit;
        }
        // TODO goon to verify

        $info = array(
            'link_name' => $link_name,
            'link_url' => $link_url,
            'link_type_radio' => $link_type_radio,
            'link_image' => $link_image,
            'link_order' => $link_order,
            'link_state_radio' => $link_state_radio,
            'link_starttime' => $link_starttime,
            'link_endtime' => $link_endtime,
            'link_target' => $link_target,
            'link_description' => $link_description,
            'link_time' => time()
        );

        if ($link_id > 0) {
            $link_page = HttpResponse::getCookie('link_page');
            //update save article
            $where = 'link_id = ' . $link_id . '';
            $rs = $this->tmp_model->addlink($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改友情链接成功', PHP_SELF . '?m=link&page= ' . $link_page . '');
            } else {
                $this->redirect('修改友情链接失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addlink($info);
            if ($rs) {
                $this->redirect('添加友情链接成功', PHP_SELF . '?m=link');
            } else {
                $this->redirect('添加友情链接失败');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $do_id = (int) $_GET['link_id'];
        $id_a = !empty($_POST['id_a']) ? $_POST['id_a'] : '';
        if (is_array($id_a)) {
            $id = implode(',', $id_a);
        } elseif (!empty($do_id)) {
            $id = $do_id;
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->dellink($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $link_page = HttpResponse::getCookie('link_page');
            $this->redirect('删除友情链接成功', PHP_SELF . '?m=link&page=' . $link_page . '');
        } else {
            $this->redirect('删除友情链接失败，请重试！');
        }
    }

}